﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JSC.Portal.Web.App.Modules.Articles.Services;
using JSC.Portal.Web.App.Modules.Articles.Entities;
using JSC.Portal.Entities;
using System.Web.UI.WebControls;
using JSC.Core.Data;
using NHibernate;

namespace JSC.Portal.Web.App.Modules.Articles.Data
{
    public class ArticleService : CommonDao<Article>, IArticleService
    {
        #region IArticleService Members

        public IList<Article> GetArticles(int sectionId, SortBy sortBy, SortDirection sortDirection)
        {
            string hql = "from Article a where a.Section.Id = :sectionId "
                + GetOrderByClause(sortBy, sortDirection, "a");
            IQuery q = this.GetQuery(hql);
            q.SetInt32("sectionId", sectionId);
            return q.List<Article>();
        }

        public IList<Article> GetRssArticles(Section section, int maxNumberOfArticles, SortBy sortBy, SortDirection sortDirection)
        {
            string hql = "from Article a where a.Section.Id = :sectionId and a.Syndicate = :syndicate and a.ValidFrom < :now and (a.ValidTill > :now or a.ValidTill is null) "
                + GetOrderByClause(sortBy, sortDirection, "a");
            IQuery q = this.GetQuery(hql);
            q.SetInt32("sectionId", section.Id);
            q.SetBoolean("syndicate", true);
            q.SetDateTime("now", DateTime.Now);
            q.SetMaxResults(maxNumberOfArticles);
            return q.List<Article>();
        }


        public IList<Article> GetRssArticlesByCategory(ArticleCategory category, SortBy sortBy, SortDirection sortDirection)
        {
            string hql = "from Article a where a.ArticleCategory.Id = :categoryId and a.Syndicate = :syndicate and a.ValidFrom < :now and a.ValidTill > :now "
                + GetOrderByClause(sortBy, sortDirection, "a");
            IQuery q = this.GetQuery(hql);
            q.SetInt32("categoryId", category.Id);
            q.SetBoolean("syndicate", true);
            q.SetDateTime("now", DateTime.Now);
            return q.List<Article>();
        }

        public IList<ArticleCategory> GetArticleCategoriesBySection(Section section)
        {
            string hql = "from Article a where a.Section.Id = :sectionId";
            return null;
        }

        #endregion

        private string GetOrderByClause(SortBy sortBy, SortDirection sortDirection, string articleAlias)
        {
            string sortDir = (sortDirection == SortDirection.Ascending) ? "ASC" : "DESC";
            if (sortBy == SortBy.None)
            {
                return String.Empty;
            }
            else
            {
                switch (sortBy)
                {
                    case SortBy.CreatedOn:
                    case SortBy.ModifiedOn:
                    case SortBy.ValidFrom:
                    case SortBy.Title:
                        return String.Format("order by {0}.{1} {2}", articleAlias, sortBy.ToString(), sortDir);
                    case SortBy.ArticleCategory:
                        return String.Format("order by {0}.ArticleCategory.Name {1}", articleAlias, sortDir);
                    case SortBy.CreatedByUser:
                        return String.Format("order by {0}.CreatedByUser.UserName {1}", articleAlias, sortDir);
                    case SortBy.ModifiedByUser:
                        return String.Format("order by {0}.ModifiedByUser.UserName {1}", articleAlias, sortDir);
                    default:
                        return String.Empty;
                }
            }
        }
    }
}
